<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\Admin;

use App\Controller\AppBaseController;

use App\Model\Admin;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Controller(prefix: 'admin')]
class AuthController extends AppBaseController
{
    /**
     * 后台--登录
     * @return object
     */
    #[RequestMapping(path:'login',methods: 'post,get')]
    public function login(): object
    {
        $username = $this->request->input('username',null);
        $password = $this->request->input('password',null);

        if(empty($username) || empty($password)) return $this->reMsg->re(0,'信息不能为空');
        //查新管理员信息状态
        $adminInfo = Admin::where(['username'=>$username, 'password'=>md5($password)])
            ->selectRaw('id,username,phone,level,party_id,status')
            ->first();
        if(empty($adminInfo)) return $this->reMsg->re(0,'账号或密钥错误');
        if($adminInfo->status==0) return $this->reMsg->re(0,'管理员被禁用');
        $_admin = new \stdClass();
        $_admin->id = $adminInfo->id;
        $_admin->username = $adminInfo->username;
        $_admin->level = $adminInfo->level;
        $_admin->party_id = $adminInfo->party_id;
        $_admin->app_env = $this->app_env;
        //将信息存入session
        $this->session->set('api_admin',$_admin);
        $this->session->save();
        return $this->reMsg->re(1,'success',$_admin);
    }


    /**
     * 后台--退出登录
     * @return object
     */
    #[RequestMapping(path:'loginOut',methods: 'post,get')]
    public function loginOut(): object
    {
        $this->session->set('api_admin',null);
        $this->session->save();
        return $this->reMsg->re(1);
    }


}